<template>
  <div>
     <Row>
       <label>国家:</label>
        <Select v-model="area" style="width:200px" not-found-text="-1">
          <Option v-for="item in tableList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
        <Date-picker v-model="startDay" placeholder="开始日期" format="yyyy-MM-dd"></Date-picker>
        <Date-picker v-model="endDay" placeholder="结束日期" format="yyyy-MM-dd"></Date-picker>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" @click="onRefresh">刷新统计</Button>
    </Row>
    </br>
    <Table :border="true" :stripe="true" :show-header="true" :height="500" :data="tableData" :columns="tableColumns"></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
  </div>
</template>
<script>

import {getRoleKeepAnalysArea, refreshRoleKeepAnalysArea} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
export default {
  data () {
    return {
      tableData: [],
      startDay: '',
      endDay: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      area: '-1',
      tableList: [
        {
          value: '-1',
          label: '全国家'
        },
        {
          value: '美国',
          label: '美国'
        },
        {
          value: '加拿大',
          label: '加拿大'
        },
        {
          value: '澳大利亚',
          label: '澳大利亚'
        },
        {
          value: '英国',
          label: '英国'
        },
        {
          value: '新加坡',
          label: '新加坡'
        },
        {
          value: '新西兰',
          label: '新西兰'
        },
        {
          value: '德国',
          label: '德国'
        },
        {
          value: '印度',
          label: '印度'
        },
        {
          value: '罗马尼亚',
          label: '罗马尼亚'
        },
        {
          value: '缅甸',
          label: '缅甸'
        },
        {
          value: '台湾',
          label: '台湾'
        },
        {
          value: '意大利',
          label: '意大利'
        },
        {
          value: '中国',
          label: '中国'
        },
        {
          value: '希腊',
          label: '希腊'
        },
        {
          value: '墨西哥',
          label: '墨西哥'
        },
        {
          value: '法国',
          label: '法国'
        },
        {
          value: '菲律宾',
          label: '菲律宾'
        },
        {
          value: '波兰',
          label: '波兰'
        },
        {
          value: '马来西亚',
          label: '马来西亚'
        },
        {
          value: '南非',
          label: '南非'
        }
      ]
    }
  },
  created () {
    this.endDay = new Date()
    this.startDay = new Date().setDate(new Date().getDate() - 7)
    this.onSearch()
  },
  methods: {
    onSearch () {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      console.log('area:', this.area, ',sSendDay:', sSendDay, ',eSendDay:', eSendDay, ',crtPage:', this.crtPage, ',pageSize:', this.pageSize)
      getRoleKeepAnalysArea(sSendDay, eSendDay, this.area, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let RoleKeepAnalysAreaVO = getVO('RoleKeepAnalysAreaVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = RoleKeepAnalysAreaVO.decode(pv.data[i])
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onRefresh () {
      refreshRoleKeepAnalysArea().then((data) => {
        if (!data) {
          return
        }
        this.$Message.info('刷新成功!!!')
        this.onSearch()
        console.log('refresh role keep analys area end')
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: '时间',
          key: 'day',
          align: 'center'
        },
        {
          title: '区域',
          key: 'area',
          align: 'center'
        },
        {
          title: '登录人数',
          key: 'loginCount',
          align: 'center'
        },
        {
          title: '新增人数',
          key: 'addCount',
          align: 'center'
        },
        {
          title: '次留存',
          key: 'keepRate1',
          align: 'center'
        },
        {
          title: '3留存',
          key: 'keepRate3',
          align: 'center'
        },
        {
          title: '7留存',
          key: 'keepRate7',
          align: 'center'
        },
        {
          title: '14留存',
          key: 'keepRate14',
          align: 'center'
        },
        {
          title: '30留存',
          key: 'keepRate30',
          align: 'center'
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}
</style>
